﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Corona.TagContract;

namespace Corona.TagToolkit
{
    /// <summary>
    /// 此类封装了与服务器交互的方法
    /// </summary>
    public class ServerSupport
    {
        ITagServer server;
        ChannelFactory<ITagServer> factory;

        internal ChannelFactory<ITagServer> Factory
        {
            get { return factory; }
            set { factory = value; }
        }
        /// <summary>
        /// 服务端，提供相应方法
        /// </summary>
        internal ITagServer Server
        {
            get { return server; }
        }


        /// <summary>
        /// 初始化服务器
        /// </summary>
        /// <param name="address">服务地址，若为空，则为http://localhost:5656/TagServer.svc</param>
        internal void setServer(string address)
        {
            address = address == ""?"http://localhost:5656/TagServer.svc":address;
            factory = new ChannelFactory<ITagServer>(new WSHttpBinding(), new EndpointAddress(address));

            server = factory.CreateChannel();
            
        }

        internal void shutdown()
        {
            factory.Close();
        }

        internal static IEnumerable<Corona.TagContract.Tag> convertTags(IEnumerable<Corona.TagToolkit.Tag> toolkitTags)
        {

            foreach (Corona.TagContract.Tag tag in toolkitTags)
                yield return tag;
        }

    }

}
